using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VTPUID
    {
        public DataRow Convert2TPUID(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryTPUID(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["TPUMANDANT"] = " ".PadRight(2 ); 
         dwReturn["TPUTUID"] = " ".PadRight(8 ); 
         dwReturn["TPUAENDZL"] = " ".PadRight(0 ); 
         dwReturn["TPUAENDBEN"] = " ".PadRight(8 ); 
         dwReturn["TPUAENDDAT"] = " ".PadRight(0 ); 
         dwReturn["TPUNEUBEN"] = " ".PadRight(8 ); 
         dwReturn["TPUNEUDAT"] = " ".PadRight(0 ); 
         dwReturn["TPUSTAKZ"] = " ".PadRight(1 ); 
         dwReturn["TPUTYP"] = " ".PadRight(4 ); 
         dwReturn["TPULO"] = " ".PadRight(2 ); 
         dwReturn["TPULB"] = " ".PadRight(2 ); 
         dwReturn["TPULP"] = " ".PadRight(8 ); 
         dwReturn["TPUHLP"] = " ".PadRight(8 ); 
         dwReturn["TPUZLO"] = " ".PadRight(2 ); 
         dwReturn["TPUZLB"] = " ".PadRight(2 ); 
         dwReturn["TPUZLP"] = " ".PadRight(8 ); 
         dwReturn["TPUHEIGHT"] = " ".PadRight(0 ); 
         dwReturn["TPUWEIGHT"] = " ".PadRight(0 ); 
         dwReturn["TPUMVDATE"] = " ".PadRight(0 ); 
         dwReturn["TPUCOM"] = " ".PadRight(80 ); 
         dwReturn["TPUMVTIME"] = " ".PadRight(0 ); 
         dwReturn["TPUAENDTIME"] = " ".PadRight(0 ); 
         dwReturn["TPUDIVARTKZ"] = " ".PadRight(1 ); 
         dwReturn["TPUKUNDNR"] = " ".PadRight(8 ); 
         dwReturn["TPUWERK"] = " ".PadRight(3 ); 
         dwReturn["TPUABT"] = " ".PadRight(3 ); 
         dwReturn["TPUWAKZ"] = " ".PadRight(1 ); 
         dwReturn["TPUART"] = " ".PadRight(20 ); 
         dwReturn["TPUVAR"] = " ".PadRight(3 ); 
         dwReturn["TPUDPART"] = " ".PadRight(11 ); 
         dwReturn["TPUSTKNR"] = " ".PadRight(12 ); 
         dwReturn["TPUNETGEW"] = " ".PadRight(0 ); 
         dwReturn["TPULGMTR"] = " ".PadRight(0 ); 
         dwReturn["TPUAUFKRS"] = " ".PadRight(2 ); 
         dwReturn["TPUAUFLFD"] = " ".PadRight(6 ); 
         dwReturn["TPUAUFPOS"] = " ".PadRight(3 ); 
         dwReturn["TPUKZLEER"] = " ".PadRight(1 ); 
         dwReturn["TPULWDATE"] = " ".PadRight(0 ); 
         dwReturn["TPULWTIME"] = " ".PadRight(0 ); 
         dwReturn["TPUEWLFDNR"] = " ".PadRight(0 ); 
         dwReturn["TPURWLFDNR"] = " ".PadRight(0 ); 
         dwReturn["TPUBSTIPKT"] = " ".PadRight(0 ); 
         dwReturn["TPUFILLER"] = " ".PadRight(79 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapTPUID(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveTPUID(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryTPUID(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapTPUID(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertTPUID(dwLocal, ITXDBVERSION);
            }
            UpdateTPUID(dwLocal);
        }
        public void InsertTPUID(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO TPUID(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryTPUID(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM TPUID WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM TPUID  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "TPUID";
            Return = Result;
            return Return;
        }
        public void DeleteTPUID(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM TPUID   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateTPUID(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE TPUID SET "
                    +"TPUMANDANT= :TPUMANDANT,"
                    +"TPUTUID= :TPUTUID,"
                    +"TPUAENDZL= :TPUAENDZL,"
                    +"TPUAENDBEN= :TPUAENDBEN,"
                    +"TPUAENDDAT= :TPUAENDDAT,"
                    +"TPUNEUBEN= :TPUNEUBEN,"
                    +"TPUNEUDAT= :TPUNEUDAT,"
                    +"TPUSTAKZ= :TPUSTAKZ,"
                    +"TPUTYP= :TPUTYP,"
                    +"TPULO= :TPULO,"
                    +"TPULB= :TPULB,"
                    +"TPULP= :TPULP,"
                    +"TPUHLP= :TPUHLP,"
                    +"TPUZLO= :TPUZLO,"
                    +"TPUZLB= :TPUZLB,"
                    +"TPUZLP= :TPUZLP,"
                    +"TPUHEIGHT= :TPUHEIGHT,"
                    +"TPUWEIGHT= :TPUWEIGHT,"
                    +"TPUMVDATE= :TPUMVDATE,"
                    +"TPUCOM= :TPUCOM,"
                    +"TPUMVTIME= :TPUMVTIME,"
                    +"TPUAENDTIME= :TPUAENDTIME,"
                    +"TPUDIVARTKZ= :TPUDIVARTKZ,"
                    +"TPUKUNDNR= :TPUKUNDNR,"
                    +"TPUWERK= :TPUWERK,"
                    +"TPUABT= :TPUABT,"
                    +"TPUWAKZ= :TPUWAKZ,"
                    +"TPUART= :TPUART,"
                    +"TPUVAR= :TPUVAR,"
                    +"TPUDPART= :TPUDPART,"
                    +"TPUSTKNR= :TPUSTKNR,"
                    +"TPUNETGEW= :TPUNETGEW,"
                    +"TPULGMTR= :TPULGMTR,"
                    +"TPUAUFKRS= :TPUAUFKRS,"
                    +"TPUAUFLFD= :TPUAUFLFD,"
                    +"TPUAUFPOS= :TPUAUFPOS,"
                    +"TPUKZLEER= :TPUKZLEER,"
                    +"TPULWDATE= :TPULWDATE,"
                    +"TPULWTIME= :TPULWTIME,"
                    +"TPUEWLFDNR= :TPUEWLFDNR,"
                    +"TPURWLFDNR= :TPURWLFDNR,"
                    +"TPUBSTIPKT= :TPUBSTIPKT,"
                    +"TPUFILLER= :TPUFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("TPUMANDANT", OracleType.NVarChar).Value = dwData["TPUMANDANT"].ToString(); 
          or1.Parameters.Add("TPUTUID", OracleType.NVarChar).Value = dwData["TPUTUID"].ToString(); 
          or1.Parameters.Add("TPUAENDZL", OracleType.NVarChar).Value = dwData["TPUAENDZL"].ToString(); 
          or1.Parameters.Add("TPUAENDBEN", OracleType.NVarChar).Value = dwData["TPUAENDBEN"].ToString(); 
          or1.Parameters.Add("TPUAENDDAT", OracleType.NVarChar).Value = dwData["TPUAENDDAT"].ToString(); 
          or1.Parameters.Add("TPUNEUBEN", OracleType.NVarChar).Value = dwData["TPUNEUBEN"].ToString(); 
          or1.Parameters.Add("TPUNEUDAT", OracleType.NVarChar).Value = dwData["TPUNEUDAT"].ToString(); 
          or1.Parameters.Add("TPUSTAKZ", OracleType.NVarChar).Value = dwData["TPUSTAKZ"].ToString(); 
          or1.Parameters.Add("TPUTYP", OracleType.NVarChar).Value = dwData["TPUTYP"].ToString(); 
          or1.Parameters.Add("TPULO", OracleType.NVarChar).Value = dwData["TPULO"].ToString(); 
          or1.Parameters.Add("TPULB", OracleType.NVarChar).Value = dwData["TPULB"].ToString(); 
          or1.Parameters.Add("TPULP", OracleType.NVarChar).Value = dwData["TPULP"].ToString(); 
          or1.Parameters.Add("TPUHLP", OracleType.NVarChar).Value = dwData["TPUHLP"].ToString(); 
          or1.Parameters.Add("TPUZLO", OracleType.NVarChar).Value = dwData["TPUZLO"].ToString(); 
          or1.Parameters.Add("TPUZLB", OracleType.NVarChar).Value = dwData["TPUZLB"].ToString(); 
          or1.Parameters.Add("TPUZLP", OracleType.NVarChar).Value = dwData["TPUZLP"].ToString(); 
          or1.Parameters.Add("TPUHEIGHT", OracleType.NVarChar).Value = dwData["TPUHEIGHT"].ToString(); 
          or1.Parameters.Add("TPUWEIGHT", OracleType.NVarChar).Value = dwData["TPUWEIGHT"].ToString(); 
          or1.Parameters.Add("TPUMVDATE", OracleType.NVarChar).Value = dwData["TPUMVDATE"].ToString(); 
          or1.Parameters.Add("TPUCOM", OracleType.NVarChar).Value = dwData["TPUCOM"].ToString(); 
          or1.Parameters.Add("TPUMVTIME", OracleType.NVarChar).Value = dwData["TPUMVTIME"].ToString(); 
          or1.Parameters.Add("TPUAENDTIME", OracleType.NVarChar).Value = dwData["TPUAENDTIME"].ToString(); 
          or1.Parameters.Add("TPUDIVARTKZ", OracleType.NVarChar).Value = dwData["TPUDIVARTKZ"].ToString(); 
          or1.Parameters.Add("TPUKUNDNR", OracleType.NVarChar).Value = dwData["TPUKUNDNR"].ToString(); 
          or1.Parameters.Add("TPUWERK", OracleType.NVarChar).Value = dwData["TPUWERK"].ToString(); 
          or1.Parameters.Add("TPUABT", OracleType.NVarChar).Value = dwData["TPUABT"].ToString(); 
          or1.Parameters.Add("TPUWAKZ", OracleType.NVarChar).Value = dwData["TPUWAKZ"].ToString(); 
          or1.Parameters.Add("TPUART", OracleType.NVarChar).Value = dwData["TPUART"].ToString(); 
          or1.Parameters.Add("TPUVAR", OracleType.NVarChar).Value = dwData["TPUVAR"].ToString(); 
          or1.Parameters.Add("TPUDPART", OracleType.NVarChar).Value = dwData["TPUDPART"].ToString(); 
          or1.Parameters.Add("TPUSTKNR", OracleType.NVarChar).Value = dwData["TPUSTKNR"].ToString(); 
          or1.Parameters.Add("TPUNETGEW", OracleType.NVarChar).Value = dwData["TPUNETGEW"].ToString(); 
          or1.Parameters.Add("TPULGMTR", OracleType.NVarChar).Value = dwData["TPULGMTR"].ToString(); 
          or1.Parameters.Add("TPUAUFKRS", OracleType.NVarChar).Value = dwData["TPUAUFKRS"].ToString(); 
          or1.Parameters.Add("TPUAUFLFD", OracleType.NVarChar).Value = dwData["TPUAUFLFD"].ToString(); 
          or1.Parameters.Add("TPUAUFPOS", OracleType.NVarChar).Value = dwData["TPUAUFPOS"].ToString(); 
          or1.Parameters.Add("TPUKZLEER", OracleType.NVarChar).Value = dwData["TPUKZLEER"].ToString(); 
          or1.Parameters.Add("TPULWDATE", OracleType.NVarChar).Value = dwData["TPULWDATE"].ToString(); 
          or1.Parameters.Add("TPULWTIME", OracleType.NVarChar).Value = dwData["TPULWTIME"].ToString(); 
          or1.Parameters.Add("TPUEWLFDNR", OracleType.NVarChar).Value = dwData["TPUEWLFDNR"].ToString(); 
          or1.Parameters.Add("TPURWLFDNR", OracleType.NVarChar).Value = dwData["TPURWLFDNR"].ToString(); 
          or1.Parameters.Add("TPUBSTIPKT", OracleType.NVarChar).Value = dwData["TPUBSTIPKT"].ToString(); 
          or1.Parameters.Add("TPUFILLER", OracleType.NVarChar).Value = dwData["TPUFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
